package com.crazydream.wechat.system.model;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.crazydream.wechat.common.model.TableEntity;

@Entity
@NamedQueries( { @NamedQuery(name = User.CHECK_USER, query = "select count(*) from User where username =:username"),
	@NamedQuery(name = User.QUERY_BY_USER_NAME, query = "from User u where u.username =:username") })
@Table(name = "t_user")
public class User extends TableEntity implements UserDetails{

	private static final long serialVersionUID = -7837575515918788475L;
	
	public static final String CHECK_USER = "User.checkUser";
	
	public static final String QUERY_BY_USER_NAME= "User.queryByUserName";

	private String username;
	
	private String password;
	
	private String name;
	
	private String lastip;
	
	private Integer logincount;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastdate;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date createdate;
	
	private Integer status;
	
	@ManyToMany(targetEntity=Role.class,fetch=FetchType.EAGER)
	@JoinTable(name = "t_user_role",
			   joinColumns = {@JoinColumn(name = "userid")} ,
			   inverseJoinColumns = {@JoinColumn(name = "roleid")})
	private List<Role> roleList;
	
	//安全模块
	@Transient
	private boolean accountNonExpired = true; // 账号是否过期
	@Transient
	private boolean accountNonLocked = true; // 账号是否锁定
	@Transient
	private boolean credentialsNonExpired = true; // 
	@Transient
	private boolean enabled = true; // 是否可用
	@Transient
	private Collection<? extends GrantedAuthority> authorities; // 权限集合
	
	// 获取当前登录用户详细信息必须重写hashCode和equals方法
	public int hashCode() {
		return this.getUsername().hashCode();
	}

	public boolean equals(Object object) {
		boolean flag = false;
		if (object instanceof UserDetails) {
			UserDetails user = (UserDetails) object;
			if (user.getUsername().equals(this.getUsername()))
				flag = true;
		}
		return flag;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLastip() {
		return lastip;
	}

	public void setLastip(String lastip) {
		this.lastip = lastip;
	}

	public Integer getLogincount() {
		return logincount;
	}

	public void setLogincount(Integer logincount) {
		this.logincount = logincount;
	}

	public Date getLastdate() {
		return lastdate;
	}

	public void setLastdate(Date lastdate) {
		this.lastdate = lastdate;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}

	public void setAccountNonExpired(boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}

	public void setAccountNonLocked(boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}

	public void setCredentialsNonExpired(boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	
	
}
